const {
  Mysql,
  addSql,
  xiuSql,
  remSql,
  execute,
  paraphrase,
} = require("../../db/db");
// 获取当前登录用户的账号信息
let getToken = async (ctx, next) => {
  // 接收用户id
  let { uid } = ctx.request.body;
  let res = [];
  let likes_num = 0;
  // 获取总积分在我的页渲染
  res = await Mysql("userlist", { uid });
  // 获取收藏总数在我的页渲染
  likes_num = await Mysql("likes", { uid });
  likes_num = paraphrase(likes_num).length;
  ctx.body = {
    msg: "请求成功",
    code: "01",
    likes_num,
    data: paraphrase(res)[0],
  };
  await next();
};
// 返回签到页数据
let sign = async (ctx, next) => {
  let res = await Mysql("list");
  ctx.body = {
    msg: "请求成功",
    code: "01",
    data: paraphrase(res),
  };
  await next();
};
// 返回积分兑换页数据
let hsignlist = async (ctx, next) => {
  let res = await Mysql("listdata");
  ctx.body = {
    msg: "请求成功",
    code: "01",
    data: paraphrase(res),
  };
  await next();
};
// 签到页数据 根据点击签到的是那天的修改对应数据 并返回修改之后的数据
let signss = async (ctx, next) => {
  let { id } = ctx.request.body;
  console.log(id);
  //   首先修改签到状态
  await xiuSql("list", { flag: true }, { id });
  //   然后再把修改之后的数据返回给前台
  let res = await Mysql("list");
  ctx.body = {
    msg: "请求成功",
    code: "01",
    data: paraphrase(res),
  };
  await next();
};
// 待收货等... 请求
let sites = async (ctx, next) => {
  let { id } = ctx.request.body;
  console.log(id);
  // 查找对应数据
  // let res = await Mysql("list");
  ctx.body = {
    msg: "请求成功",
    code: "01",
    //   data: paraphrase(res),
  };
  await next();
};
// 点击收藏 根据前台传过来的用户id 返回对应收藏的商品
let cillse = async (ctx, next) => {
  let { uid } = ctx.request.body;
  console.log(uid);
  // 查找对应数据
  let res = await Mysql("likes", { uid });
  //   根据对应数据的商品id查找对应商品 并返回
  ctx.body = {
    msg: "请求成功",
    code: "01",
    data: paraphrase(res),
  };
  await next();
};
// 点击保存地址修改地址删除地址 把前台发来的地址保存导数据库
let honSave = async (ctx, next) => {
  let { id, uid, date, type } = ctx.request.body;
  console.log(uid, date);
  console.log("请求成功");
  let res = [];
  // 判断点击的是不是删除按钮
  if (type) {
    // 是
    res = await remSql("shopping_address", { id });
    // 判断删除的是不是 默认地址
    // 查找所有地址
    res = await Mysql("shopping_address", { uid });
    if (date.isDefault) {
      // 是
      console.log(paraphrase(res)[0]);
      // 判断还有没有地址
      if (paraphrase(res).length) {
        // 有
        // paraphrase(res)[0];
        // 直接把第一条地址设置为 默认地址
        res = await xiuSql(
          "shopping_address",
          { isDefault: true },
          { id: paraphrase(res)[0].id }
        );
      } else {
        // 已经没有地址了
        ctx.body = {
          msg: "请求成功,暂时没有地址",
          code: "01",
        };
      }
    } else {
      // 判断还有没有地址
      if (!paraphrase(res).length) {
        // 已经没有地址了
        ctx.body = {
          msg: "请求成功,暂时没有地址",
          code: "01",
        };
      }
    }
  } else {
    // 拼接收货地址
    let address = `${date.province}${date.city}${date.county}  ${date.addressDetail}`;
    //首先判断是否设为默认地址
    if (date.isDefault) {
      // 是
      //把当前的默认地址的状态改为false
      await execute(
        `UPDATE shopping_address SET isDefault=REPLACE(isDefault,'true','false')`
      );
    }
    //判断是修改地址还是添加地址
    if (id) {
      //编辑地址
      //不是
      res = await xiuSql(
        "shopping_address",
        { name: date.name, tel: date.tel, address, isDefault: date.isDefault },
        { id }
      );
    } else {
      //添加地址
      console.log(address);
      // 直接保存大数据库
      await addSql("shopping_address", {
        uid,
        name: date.name,
        tel: date.tel,
        address,
        isDefault: date.isDefault,
      });
    }
  }

  ctx.body = {
    msg: "请求成功",
    code: "01",
  };
  await next();
};
// 获取所有地址接口
let sitetgh = async (ctx, next) => {
  // 接收前台发来的当前用户id
  let { uid } = ctx.request.body;
  console.log(uid);
  //   查找对应地址 并返回
  let res = await Mysql("shopping_address", { uid });
  ctx.body = {
    msg: "请求成功",
    code: "01",
    data: paraphrase(res),
  };
  await next();
};
// 点击地址也单选框 修改默认地址状态
let hsiteselect = async (ctx, next) => {
  // 接收前台发来的当前用户id
  let { id, uid } = ctx.request.body;
  console.log(id, uid);
  //   查找对应地址
  let res = await execute(
    `SELECT * from shopping_address where uid=${uid} and id=${id}`
  );
  //把所有地址的默认状态改为false
  await execute(
    `UPDATE shopping_address SET isDefault=REPLACE(isDefault,'true','false')`
  );
  //把当前的默认地址的状态改为false
  await xiuSql(
    "shopping_address",
    { isDefault: true },
    { id: paraphrase(res)[0].id }
  );
  ctx.body = {
    msg: "请求成功",
    code: "01",
    data: paraphrase(await Mysql("shopping_address")),
  };
  await next();
};
// 修改用户信息请求
let hpictur = async (ctx, next) => {
  // 接收前台发来的当前用户id
  let { uid, img, name, gender } = ctx.request.body;
  console.log("--------------------");
  console.log(uid, img, gender);
  let res = [];
  // 判断修改的是不是昵称
  if (name) {
    // 是
    // 只改昵称
    await xiuSql("userlist", { name }, { uid });
  } else {
    // 不是
    // 不该昵称
    await xiuSql("userlist", { img, gender }, { uid });
  }
  res = ctx.body = {
    msg: "请求成功",
    code: "01",
    data: paraphrase(await Mysql("userlist", { uid }))[0],
  };
  await next();
};
// 收藏页数据请求
let hcillse = async (ctx, next) => {
  // 接收前台发来的当前用户id   and likes.goods_id=shopping.goods_id
  let { uid } = ctx.request.body;
  let res = [];
  console.log(uid);
  res = await execute(
    `select s.* from likes l,goods s WHERE l.uid=${uid} and s.goods_id=l.goods_id`
  );
  // console.log(paraphrase(res));
  ctx.body = {
    msg: "请求成功",
    code: "01",
    data: paraphrase(res),
  };
  await next();
};
// 收藏页添加商品到购物车页
let cartpes = async (ctx, next) => {
  // 接收前台发来的当前用户id和商品
  let { uid, item } = ctx.request.body;
  let res = [];
  item.uid = uid;
  console.log(ctx.request.body);
  // 查找购物车里有没有该用户买的这个商品
  res = await execute(
    `SELECT * from shopping where uid=${uid} and goods_id=${item.goods_id}`
  );
  res = paraphrase(res);
  if (res.length) {
    // 有
    // 让该用户买的这个商品数量加1
    await xiuSql(
      "shopping",
      {
        goods_number: res[0].goods_number + 1,
      },
      { goods_id: item.goods_id }
    );
  } else {
    // 没有
    // 直接添加这条商品
    res = await addSql("shopping", item);
    console.log(paraphrase(res));
  }
  ctx.body = {
    msg: "添加成功",
    code: "01",
    // data: paraphrase(res),
  };
  await next();
};
// 收藏页删除收藏的商品
let remomo = async (ctx, next) => {
  // 接收前台发来的当前用户id和商品
  let { uid, goods_id } = ctx.request.body;
  let res = [];
  console.log(ctx.request.body);
  res = await execute(
    `delete from likes where uid=${uid} and goods_id=${goods_id}`
  );
  console.log(paraphrase(res));
  ctx.body = {
    msg: "删除成功",
    code: "01",
  };
  await next();
};
// 猜你喜欢 随机获取数据
let cillseers = async (ctx, next) => {
  // num代表要获取几条数据
  let { num } = ctx.request.body;
  let res = [];
  let select = [];
  console.log(ctx.request.body);
  //首先获取所有数据
  res = await Mysql("goods");
  // 前台一次想获取几条数据循环几次
  for (let i = 0; i < num; i++) {
    // 每次随机生成数据长度的随机数
    let suijiNum = Math.floor(Math.random() * paraphrase(res).length);
    // 分别把每次获取到的数据添加到一个新数组里边
    // 并返回给前端
    select.push(paraphrase(res)[suijiNum]);
  }
  ctx.body = {
    msg: "请求成功",
    code: "01",
    data: select,
  };
  await next();
};
// 修改用户总积分
let integer = async (ctx, next) => {
  // 接收积分
  let { uid, num } = ctx.request.body;
  let res = [];
  let listChecked = [];
  console.log(ctx.request.body);
  await execute(`UPDATE userlist SET integral=${num} WHERE uid=${uid}`);
  res = await Mysql("userlist", { uid });
  // 返回以前到的天数
  listChecked = await Mysql("list", { flag: "true" });
  ctx.body = {
    msg: "请求成功",
    code: "01",
    listChecked: paraphrase(listChecked).length,
    data: paraphrase(res)[0],
  };
  await next();
};
// 积分兑换商品
let conver = async (ctx, next) => {
  // 接收积分商品的id
  let { uid, list_id } = ctx.request.body;
  let res = [];
  console.log(ctx.request.body);
  res = addSql("shopping_list", { uid, list_id });
  ctx.body = {
    msg: "请求成功",
    code: "01",
    data: paraphrase(res)[0],
  };
  await next();
};
// 获取总积分接口
let getintegral = async (ctx, next) => {
  // 接收积分
  let { uid } = ctx.request.body;
  let res = [];
  console.log(ctx.request.body);
  res = addSql("userlist", { uid });
  ctx.body = {
    msg: "请求成功",
    code: "01",
    data: paraphrase(res)[0],
  };
  await next();
};
// 获取订单状态数据
let shopdata = async (ctx, next) => {
  // 当前用户id/要获取的订单状态的类型
  let { uid, index, goods_id } = ctx.request.body;
  let res = [];
  let data = [];
  console.log(ctx.request.body);
  if (index === 5) {
    data = await Mysql("shopping", { uid });
  } else if (goods_id) {
    if (index === 0) {
      // res = await execute(
      //   `SELECT * from shopping where uid=${uid} and goods_id=${goods_id}`
      // );
      console.log(
        paraphrase(
          await execute(
            `update shopping set num1=1 where uid=${uid} and goods_id=${goods_id}`
          )
        )
      );
    } else if (index === 1) {
    } else if (index === 2) {
    } else if (index === 3) {
    }
  }
  res = await execute(
    `SELECT * from shopping where uid=${uid} and num1=${index}`
  );
  console.log(paraphrase(res));
  ctx.body = {
    msg: "请求成功",
    code: "01",
    data,
    num1data: paraphrase(res),
  };
  await next();
};
module.exports = {
  sign,
  hsignlist,
  signss,
  sites,
  cillse,
  honSave,
  sitetgh,
  hsiteselect,
  hpictur,
  hcillse,
  cartpes,
  remomo,
  cillseers,
  integer,
  conver,
  getToken,
  getintegral,
  shopdata,
};
